package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.CommentDao;
import com.hziee.graduate.ticket.dao.MovieDao;
import com.hziee.graduate.ticket.dao.TicketsDao;
import com.hziee.graduate.ticket.dto.PageDTO;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Comment;
import com.hziee.graduate.ticket.entity.Movie;
import com.hziee.graduate.ticket.entity.Tickets;
import com.hziee.graduate.ticket.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {
    @Resource
    private TicketsDao ticketsDao;

    @Resource
    private CommentDao commentDao;

    @Resource
    private MovieDao movieDao;

    @Override
    @Transactional
    public R<String> addRate(Map<String, String> map) {

        // 处理评论
        long userId = Long.parseLong(map.get("userId").toString());
        long movieId = Long.parseLong(map.get("movieId").toString());
        String commentValue = map.get("commentValue");
        double rateValue = Double.parseDouble(map.get("rateValue"));
        // 查看这个人是否检票过这部电影了
        boolean isWatch = false;
        List<Tickets> tickets = ticketsDao
                .selectList(new QueryWrapper<Tickets>()
                        .eq("user_id", userId)
                        .eq("movie_id", movieId)
                        .eq("pay_status", 1));
        for (Tickets ticket : tickets) {
            if(ticket.getCheckStatus() == 1) {
                isWatch = true;
                break;
            }
        }
        if(!isWatch) return R.error("您还未观看过这部电影了");
        // 查看是否评论过这部电影
        boolean isComment = false;
        Comment selectComment = commentDao.selectOne(new QueryWrapper<Comment>()
                .eq("user_id", userId)
                .eq("movie_id", movieId));
        if(selectComment != null) return R.error("您已经评论过这部电影了");
        // 没有进行过评论且已经观影才可以进行评论的插入
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setMovieId(movieId);
        comment.setCommentContent(commentValue);
        comment.setCommentScore(rateValue);
        comment.setCreateTime(LocalDateTime.now());
        save(comment);
        // 对电影进行总评
        Movie movie = movieDao.selectById(movieId);
        Integer movieScoreNum = movie.getMovieScoreNum();
        Double movieScore = movie.getMovieScore();
        movie.setMovieScoreNum(movieScoreNum + 1);
        movie.setMovieScore((movieScoreNum * movieScore + rateValue) / (movieScoreNum + 1));
        movieDao.update(movie,new UpdateWrapper<Movie>().eq("movie_id",movie.getMovieId()));
        return R.success();
    }

    @Override
    public R<PageDTO<Comment>> getLimitCommentByMovieId(Long movieId, Long currentPage) {
        PageDTO<Comment> commentPageDTO = new PageDTO<>();

        Integer pageSize = 2;
        Long start = currentPage * pageSize;
        Long end =  start + pageSize;

        Integer commentCount = query().eq("movie_id", movieId).count();
        Long totalPage = (long) Math.ceil(commentCount * 1.0 / pageSize);

        List<Comment> limitCommentByMovieId = commentDao.getLimitCommentByMovieId(movieId, start, end);

        commentPageDTO.setCurrentPage(currentPage);
        commentPageDTO.setData(limitCommentByMovieId);
        commentPageDTO.setTotalPage(totalPage - 1);

        return R.success(commentPageDTO);
    }
}

